# _*_ coding: utf-8 _*_
# @Time:2023/11/23 16:49
# @Author:10917
# @File:formlist.py
from django import forms
from django.core.exceptions import ValidationError
from tw591 import models
from tw591.utils.basicbootstrap import BootStrapModelForm, BootStrapForm
from tw591.utils.encrypt import md5


class DepartModelForm(BootStrapModelForm):
    """ 部门ModelForm """

    class Meta:
        model = models.Department
        fields = ["title"]


class UserInfoModelForm(BootStrapModelForm):
    """ 用户ModelForm """

    class Meta:
        model = models.UserInfo
        # 定义model字段方式有： # 1. fields = "__all__" ：代表所有字段  2. exclude = ['level'] ：代表排除某个字段  3.fields = ["name"] 自定义字段
        # fields = "__all__"
        fields = ["password", "age", 'account', 'create_time', "gender", "depart"]

    def clean_password(self):
        # 自定义校验方式
        password = self.cleaned_data["password"]
        if len(password) < 6:
            raise ValidationError("密码不能小于6位数字")
        return password


class LoginForm(BootStrapForm):
    username = forms.CharField(
        label="用户名",
        widget=forms.TextInput,
        required=True
    )
    password = forms.CharField(
        label="密码",
        widget=forms.PasswordInput(render_value=True),
        required=True
    )

    code = forms.CharField(
        label="验证码",
        widget=forms.TextInput,
        required=True
    )

    # def clean_password(self):
    #     pwd = self.cleaned_data.get("password")
    #     # return md5(pwd)
    #     return pwd
